'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url admin/dls/kh/cwdz/tongji 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		const db = uniCloud.database();
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, _ } = util;
		let { uid, todayStart,recharge_source,StartTime, EndTime } = data;
		let res = { code: 0, msg: "", yjyj: 0, yjbl: 0 };
		// 业务逻辑开始-----------------------------------------------------------
        if (vk.pubfn.isNotNull(StartTime)) {
        	if (userInfo.role.includes("admin")) {
        		//查询今日充值总金额
        		res.jrczje = await vk.baseDao.sum({
        			dbName: "a_recharge", // 表名
        			fieldName: "shifu_amount", // 需要求和的字段名
        			whereJson: {
        				//支付状态必须是已支付
        				pay_status: 1,
        				tv_name: recharge_source,
        				_add_time: _.and(_.gt(StartTime), _.lt(EndTime)),
        			}
        		});
        		return res;
        	}
        	
        	if (userInfo.role.includes("middleman")) {
        		//查询今日充值总金额
        		res.jrczje = await vk.baseDao.sum({
        			dbName: "a_recharge", // 表名
        			fieldName: "shifu_amount", // 需要求和的字段名
        			whereJson: {
        				middleman_id: uid,
        				//支付状态必须是已支付
        				pay_status: 1,
        				tv_name: recharge_source,
        				_add_time: _.and(_.gt(StartTime), _.lt(EndTime)),
        			}
        		});
        		return res;
        	}
        	
        	if (userInfo.role.includes("agent")) {
        		res.jrczje = await vk.baseDao.sum({
        			dbName: "a_recharge", // 表名
        			fieldName: "shifu_amount", // 需要求和的字段名
        			whereJson: {
        				invite_code: uid,
        				//支付状态必须是已支付
        				pay_status: 1,
        				tv_name: recharge_source,
        				_add_time: _.and(_.gt(StartTime), _.lt(EndTime)),
        			}
        		});
        		return res;
        	}
			if (userInfo.role.includes("copyright")) {
				//查询今日充值总金额
				res.jrczje = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						//支付状态必须是已支付
						pay_status: 1,
						tv_name: recharge_source,
						_add_time: _.and(_.gt(StartTime), _.lt(EndTime)),
					}
				});
				return res;
			}
        }else{
			if (userInfo.role.includes("admin")) {
				//查询今日充值总金额
				res.jrczje = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						//支付状态必须是已支付
						pay_status: 1,
						tv_name: recharge_source,
					}
				});
				return res;
			}
			
			if (userInfo.role.includes("middleman")) {
				//查询今日充值总金额
				res.jrczje = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						middleman_id: uid,
						//支付状态必须是已支付
						pay_status: 1,
						tv_name: recharge_source,
					}
				});
				return res;
			}
			
			if (userInfo.role.includes("agent")) {
				res.jrczje = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						invite_code: uid,
						//支付状态必须是已支付
						pay_status: 1,
						tv_name: recharge_source,
					}
				});
				return res;
			}
			
			if (userInfo.role.includes("copyright")) {
				res.jrczje = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						//支付状态必须是已支付
						pay_status: 1,
						tv_name: recharge_source,
					}
				});
				return res;
			}
		}
		
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
